package com.shashidi.weather;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.gtdev5.geetolsdk.mylibrary.base.BaseLaunchActivity;
import com.gtdev5.geetolsdk.mylibrary.beans.DataResultBean;
import com.gtdev5.geetolsdk.mylibrary.beans.ListResultBean;
import com.gtdev5.geetolsdk.mylibrary.callback.BaseCallback;
import com.gtdev5.geetolsdk.mylibrary.util.PermissionUtils;
import com.gtdev5.geetolsdk.mylibrary.widget.OnDialogClickListener;
import com.gyf.immersionbar.ImmersionBar;
import com.shashidi.weather.bean.CityNumberBean;
import com.shashidi.weather.https.HttpDefine;
import com.shashidi.weather.util.SpDefine;

import okhttp3.Request;
import okhttp3.Response;

public class SplashActivity extends BaseLaunchActivity {

    private LocationClient mLocationClient;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_splash;
    }

    @Override
    protected void jumpToNext() {
        //获取所有站点编号列表
        getAllCity();
        //定位初始化
        initLocationClient();
        PermissionUtils.checkAndRequestPermission(this, Manifest.permission.ACCESS_FINE_LOCATION, 200,
                this::toMainActivity);
    }

    private void toMainActivity() {
        mLocationClient.start();
        new Handler().postDelayed(() -> {
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }, 2000L);
    }

    private void initLocationClient() {
        //定位初始化
        mLocationClient = new LocationClient(getApplicationContext());
        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        //设置locationClientOption
        mLocationClient.setLocOption(option);
        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 正常模式
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .navigationBarDarkIcon(true)
                .init();
        super.onCreate(savedInstanceState);
    }


    /**
     * 获取所有站点编号列表
     */
    private void getAllCity() {
        if (SpDefine.getAllCityNumbers() == null || SpDefine.getAllCityNumbers().size() == 0) {
            HttpDefine.getAllCityNumber(new BaseCallback<ListResultBean<CityNumberBean>>() {
                @Override
                public void onRequestBefore() {

                }

                @Override
                public void onFailure(Request request, Exception e) {

                }

                @Override
                public void onSuccess(Response response, ListResultBean<CityNumberBean> resultBean) {
                    if (resultBean != null && resultBean.getItems() != null && resultBean.getItems().size() > 0) {
                        SpDefine.setAllCityNumber(resultBean.getItems());
                    }
                }

                @Override
                public void onError(Response response, int errorCode, Exception e) {

                }
            });
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200) {
            PermissionUtils.onRequestPermissionResult(this, Manifest.permission.ACCESS_FINE_LOCATION, grantResults, new PermissionUtils.PermissionCheckCallBack() {
                @Override
                public void onHasPermission() {
                    toMainActivity();
                }

                @Override
                public void onUserHasAlreadyTurnedDown(String... permission) {
                    ShowTipDialog("温馨提示",
                            "授予定位权限才能获取当前位置的天气哦，点击确定继续授权",
                            "确定", new OnDialogClickListener() {
                                @Override
                                public void OnDialogOK() {
                                    PermissionUtils.requestMorePermissions(mActivity,
                                            permissions, 200);
                                }
                                @Override
                                public void OnDialogExit() {}
                            });
                }

                @Override
                public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                    ShowTipDialog("温馨提示",
                            "授予权限才能使用软件喔，请到设置中允许权限",
                            "确定", new OnDialogClickListener() {
                                @Override
                                public void OnDialogOK() {
                                    Intent mIntent = new Intent();
                                    mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    mIntent.setData(Uri.fromParts("package", getPackageName(), null));
                                    startActivityForResult(mIntent, RESULT_ACTION_SETTING);
                                }

                                @Override
                                public void OnDialogExit() {}
                            });
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            PermissionUtils.checkAndRequestPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION,
                    200, this::toMainActivity);
        }
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            String province = location.getProvince();    //获取省份
            String city = location.getCity();    //获取城市
            String district = location.getDistrict();    //获取区县
            Log.e("~~~",province+"===="+city+""+district);
            if (SpDefine.getNowCityNumber() == null) {
                CityNumberBean cityBean = new CityNumberBean();
                cityBean.city = city;
                cityBean.province = province;
                cityBean.county = district;
                SpDefine.setNowCityNumber(cityBean);
            }
        }
    }

}